package com.zbl.zblproject.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.base.Preconditions;
import com.zbl.zblproject.core.entity.RechargeLog;
import com.zbl.zblproject.core.entity.Supplier;
import com.zbl.zblproject.core.mapper.AdminUserMapper;
import com.zbl.zblproject.core.mapper.RechargeLogMapper;
import com.zbl.zblproject.core.mapper.SupplierMapper;
import com.zbl.zblproject.core.service.SupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author youlanqiang
 * @version 1.0
 * @date 2018/10/12
 * 供应商数据层
 */
@Service
@Transactional
public class SuplierServiceImpl implements SupplierService {

    @Autowired
    private SupplierMapper supplierMapper;

    @Autowired
    private RechargeLogMapper rechargeLogMapper;

    @Autowired
    private AdminUserMapper adminUserMapper;


    public IPage<Supplier> erpSupperList(int current, int size){
        Page page = new Page(current, size);
        return supplierMapper.selectPage(page);
    }


    @Override
    public boolean deleteSuppliers(List<Integer> ids) {
        return ids.size() == supplierMapper.deleteBatchIds(ids);
    }


    @Override
    public boolean updateSupplier(Supplier supplier) {
        return 1 == supplierMapper.updateById(supplier);
    }

    @Override
    public Supplier addSupplier(Supplier supplier) {
        int i =  supplierMapper.insert(supplier);
        Preconditions.checkArgument( i == 1, "更新失败");
        return supplier;
    }

    @Override
    public Supplier getOneById(int id) {
        return supplierMapper.selectById(id);
    }


    @Override
    public boolean recharge(int supplierId, int operatorId, double price) {
        if(price <= 0){
            throw new RuntimeException("充值金额太少!");
        }
        Supplier supplier = supplierMapper.selectById(supplierId);
        Preconditions.checkNotNull(supplier, "未查询到供应商!");
        BigDecimal money = BigDecimal.valueOf(price);
        RechargeLog log = new RechargeLog();
        log.setMoney(money);
        log.setBeforeMoney(supplier.getBalance());
        log.setTime(LocalDateTime.now());
        log.setUserId(supplierId);
        log.setOperatorName(adminUserMapper.selectNameById(operatorId));
        log.setMode(1);
        rechargeLogMapper.insert(log);
        return supplierMapper.deleteSuplierBalance(supplier.getSuppliersId(), money);
    }

    @Override
    public IPage<RechargeLog> logPage(int supplierId, int current, int size) {
        QueryWrapper<RechargeLog> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", supplierId).eq("mode", 1);
        IPage<RechargeLog> page = new Page<>(current, size);
        return rechargeLogMapper.selectPage(page, wrapper);
    }
}
